package com.lgqz.topsports.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;


import com.lgqz.topsports.BR;
import com.lgqz.topsports.R;
import com.lgqz.topsports.bean.Content;

import java.util.Collection;
import java.util.List;

/**
 * Description:通用RecyclerView适配器
 * Copyright(c) 2016,邱国兴
 * This Program is protected by copyright laws
 * Created by Little Monster on 2016/12/16.
 *
 * @author 邱国兴
 */

public class RecommendReadRecyclerAdapter<D> extends RecyclerView.Adapter<RecommendReadRecyclerAdapter.ViewHolder> {

    private List<D> list;
    private Context context;

    public RecommendReadRecyclerAdapter(List<D> list, Context context) {
        this.list = list;
        this.context = context;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(context), viewType, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        D d = list.get(position);
        holder.binding.setVariable(BR.content, d);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getClass().equals(Content.class)) {
            Content recsEntity = (Content) list.get(position);
            if (recsEntity.getImage_type() == 0) {
                return R.layout.item_common_two;
            } else if (recsEntity.getImage_type() == 1) {
                return R.layout.item_common_three;
            } else {
                return R.layout.item_common_one;
            }
        }
        return 0;
    }

    public void add(D d) {
        add(list.size(), d);
    }

    public void add(int position, D d) {
        if (d == null) {
            return;
        }
        list.add(position, d);
        notifyItemInserted(position);
    }

    public void addAll(Collection<? extends D> collection) {
        addAll(list.size(), collection);
    }

    private void addAll(int position, Collection<? extends D> collection) {
        if (collection == null) {
            return;
        }
        list.addAll(position, collection);
        notifyItemRangeInserted(position, collection.size());
    }

    public void remove(D d) {
        int index = list.indexOf(d);
        remove(index);
    }

    public void remove(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    public void removeAll() {
        list.clear();
        notifyDataSetChanged();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
